﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Spreadsheet;
using System.IO;

namespace COMOSOFT.OrderBusiness.Forms
{
    public partial class FrmPreviewCosting : Form
    {
        ClassShare cls = new ClassShare("FrmPreviewCosting");
        GridView GV=null;
        public FrmPreviewCosting(GridView view)
        {
            InitializeComponent();
            this.GV = view;
        }

        #region[LoadData]
        public void LoadData()
        {
            try
            {
                MemoryStream sstream = new MemoryStream();
                DevExpress.XtraPrinting.XlsExportOptions xlsOption = new DevExpress.XtraPrinting.XlsExportOptions();
                xlsOption.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;
                xlsOption.ShowGridLines = false;
                xlsOption.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;

                GV.ExportToXls(sstream, xlsOption);
                spreadsheet_Preview.LoadDocument(sstream, DocumentFormat.Xls);
                spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Columns.AutoFit(0, 100);
                //spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Rows.AutoFit(0, 100);
                spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Rows[8].Alignment.WrapText = true;
                spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Rows[9].Alignment.WrapText = true;
                for (int i = 0; i < 10; i++)
                {
                    spreadsheet_Preview.ActiveWorksheet.Rows.Insert(i, 1);
                }
                if (!File.Exists(ClassShare.lcSystem + "logo.PNG"))
                    cls.Dowload(ClassShare.ftpSystem, ClassShare.lcSystem, "logo.PNG");
                spreadsheet_Preview.ActiveWorksheet.Shapes.AddPicture(ClassShare.lcSystem + "logo.PNG", 50, 50, 800, 300, false);
                spreadsheet_Preview.ActiveWorksheet.Rows[7][7].SetValue("COSTING GANERAL");
                spreadsheet_Preview.ActiveWorksheet.Rows[7][7].Font.UnderlineType = UnderlineType.Single;
                spreadsheet_Preview.ActiveWorksheet.Rows[7][7].Font.Bold = true;
                spreadsheet_Preview.ActiveWorksheet.Rows[7][7].Font.Color = Color.Black;
                spreadsheet_Preview.ActiveWorksheet.Rows[7][7].Font.Size = 18;
                spreadsheet_Preview.ActiveWorksheet.Range["F8:L8"].FillColor = Color.LightGray;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Event]
        private void dataNavigator1_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            try
            {
                switch (e.Button.Tag.ToString())
                {
                    case "Print":
                        spreadsheet_Preview.ShowPrintDialog();
                        break;
                    case "Save":
                        String fileName = null;
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                        sfd.Title = "Save an Excel File";
                        sfd.FileName = "ConstingGaneral" + String.Format("{0:MMMddyyyy}", DateTime.Now);
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            fileName = sfd.FileName;
                            spreadsheet_Preview.SaveDocument(fileName, DocumentFormat.Xls);
                            DialogResult dlr = MessageBox.Show("Open File Export", "Do you want to open it?",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dlr == DialogResult.Yes)
                            {
                                System.Diagnostics.Process.Start(fileName);
                            }
                        }
                        break;
                    case "Refresh":
                        LoadData();
                        break;
                    case "Exit":
                        this.Close();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
